<!-- 更改性别 -->
<template>
  <div>
    <van-picker
      show-toolbar
      :columns="columns"
      @confirm="onConfirm"
      @cancel="$emit('close')"
      @change="onChange"
      :default-index="defaultIndex"
    />
  </div>
</template>

<script>
import { Toast } from "vant";
import { updateUserProfile } from "@/api/user.js";
export default {
  name: "ChangeGender",
  data() {
    return {
      columns: ["男", "女"],
      defaultIndex: this.value,
    };
  },
  props: ["value"],
  methods: {
    onChange(picker, val, index) {
      this.defaultIndex = index;
    },
    async onConfirm() {
      try {
        Toast.loading({
          forbidClick: true,
          message: "保存中..",
        });
        const { data } = await updateUserProfile({
          gender: this.defaultIndex.toString(),
        });
        Toast.success("更新成功");
        this.$emit("input", this.defaultIndex);
        this.$emit("close");
      } catch (err) {
        Toast.fail("更新失败");
        console.log(err);
      }
    },
  },
};
</script>

<style lang="less" scoped>
</style>